package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.core.domain.Maintain;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.MaintainRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;

@Api(description = "维护开关接口")
@RestController
@RequestMapping("/maintain")
public class MaintainController extends BaseController {


    @Autowired
    private MaintainRepository maintainRepository;

    @Autowired
    private UserRepository userRepository;

    @ApiOperation("查询是否跳转")
    @RequestMapping(value = "isCover", method = RequestMethod.GET)
    public JsonResponse isCover(@ApiIgnore Principal principal) throws Exception {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "系统维护中");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "系统维护中");
        }
        Maintain maintain = maintainRepository.findOne(1L);
        if (null != maintain && maintain.getCover()) {
            if (clientUser.getId().equals(1L) || clientUser.getId().equals(125L) || clientUser.getId().equals(123L) || clientUser.getId().equals(122L)) {
                return JsonResponse.successResponse();
            }
            return JsonResponse.errorResponseWithError("抱歉", "系统维护中");
        }
        return JsonResponse.successResponse();
    }


}
